/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intel.factorytest.activity;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.intel.factorytest.R;
import com.intel.factorytest.util.Util;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.app.ActionBar;
import android.app.StatusBarManager;
import android.view.Window;


/**
 * Demonstrates wrapping a layout in a ScrollView.
 *
 */
public class PointerLocation extends BaseActivity {
    private PointerLocationViewRect1 mPlvRect;
    
    ActionBar  actionBar;
    StatusBarManager mStatusBarManager;
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        /*
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_IMMERSIVE
              | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        decorView.setSystemUiVisibility(uiOptions);
        */
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        Util.wakeUpAndKeepScreenOn(this);
        Util.setWindowBrightness(this,WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.touchpanel);
        mStatusBarManager = (StatusBarManager)getSystemService(STATUS_BAR_SERVICE);
        initView();        
        HideBottomMenu();
    }

    public void HideBottomMenu(){
        mStatusBarManager.disable(StatusBarManager.DISABLE_MASK);
        actionBar = getActionBar();
        if(actionBar != null) {
               actionBar.hide();
        }
        View decorView = this.getWindow().getDecorView();        
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        actionBar = getActionBar();
                        if(actionBar != null) {
                            actionBar.hide();
                        }
                        View decorView = getWindow().getDecorView();
                        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_IMMERSIVE
                                | View.SYSTEM_UI_FLAG_FULLSCREEN;
                        decorView.setSystemUiVisibility(uiOptions);
                    }
            });
    }

    
    public void setIs_pass(boolean is_pass) {
        this.is_pass = is_pass;
    }
    
    protected void initView() {
        mSucBtn = (Button) findViewById(R.id.success_button);
        mSucBtn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                is_pass = true;
                finish();
            }
        });
        attachOnTouchListener(mSucBtn);
        
        mFailBtn = (Button) findViewById(R.id.fail_button);
        mFailBtn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                is_pass = false;
                finish();
            }
        });
        attachOnTouchListener(mFailBtn);
        
        mRepeatBtn = (Button) findViewById(R.id.repeat_button);
        mRepeatBtn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                PointerLocation.this.recreate();
            }
        });
        attachOnTouchListener(mRepeatBtn);
        
        mPlvRect = (PointerLocationViewRect1) findViewById(R.id.myrect);
        
    }
    
    @Override
    public void finish() {
        // TODO Auto-generated method stub
        tc.setTestResult(is_pass);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
        Util.setWindowBrightness(this,WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE);        
        super.onDestroy();
    }
    
}
